﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class SizeAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "size_exist_ins_tore":
                    responseData = ListSizeExistInStoreByStoreProduct();
                    break;

                case "listbytype":
                    responseData = GetListByType();
                    break;
                case "getsize":
                    responseData = GetSize();
                    break;

                case "save":
                    responseData = Save();
                    break;                
                case "delete":
                    responseData = Delete();
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData ListSizeExistInStoreByStoreProduct()
        {
            var responseData = new ResponseData();
            var storeId     = Utils.GetIdFromRequestString(Utils.StoreId, 0);
            var productId   = Utils.GetIdFromRequestString(Utils.ProductId, 0);

            try
            {
                var lstSize = SizeBo.ListSizeExistInStoreByStoreProduct(storeId, productId);

                responseData.Success = true;
                responseData.Data = lstSize;
                responseData.TotalRow = lstSize.Count;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData GetListByType()
        {
            var responseData = new ResponseData();
            var typeId = Utils.GetIdFromRequestString(Utils.TypeId, 0);
            try
            {
                var lstSize = SizeBo.GetListByType(typeId);

                responseData.Success = true;
                responseData.Data = lstSize;
                responseData.TotalRow = lstSize.Count;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData GetSize()
        {
            var responseData = new ResponseData();
            var id = Utils.GetIdFromRequestString(Utils.SizeId, 0);

            try
            {
                var size = SizeBo.GetById(id);

                responseData.Success = true;
                responseData.Data = size;
                responseData.TotalRow = 1;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Save()
        {
            var responseData = new ResponseData();

            var id       = Utils.GetIdFromRequestString(Utils.SizeId, 0);
            var name     = Utils.GetIdFromRequestString(Utils.Name, string.Empty);
            var priority = Utils.GetIdFromRequestString(Utils.Priority, 0);

            try
            {
                SizeEntity o;
                if (id > 0)
                {
                    o = SizeBo.GetById(id);
                    o.SizeId    = id;
                    o.Name      = name;
                    o.Priority  = priority;
                }
                else
                {
                    o = new SizeEntity();
                    o.Name      = name;
                    o.Priority  = priority;
                }

                SizeBo.Save(o);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var id = Utils.GetIdFromRequestString(Utils.SizeId, 0);

            try
            {
                SizeBo.Delete(id);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }    
}
